<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Publish extends CI_Controller {

	public function brief()
	{
		
		if(!$this->session->userdata('user_in'))
		{
			redirect('login/loginer');
		}
		else
		{	
			$this->load->model("type_model");
			$type = $this->type_model->selectTypeAndTypeName();
			
			//$CI = get_instance();
			//$CI->load->model('regionModel', 'region');
			//$provinces = $CI->region->provinces();
			//$citys = $CI->region->childrenOf($province_selected);
			
			$this->load->model('region_model');
			$provinces = $this->region_model->provinces();
			$citys = $this->region_model->childrenOf(6);
			
			
			$data = array('typeArr'=>$type, 
			              'province_selected'=>6,
			              'city_selected'=>76,
			              'district_selected'=>693,
			              'provinces'=>$provinces,
						  'citys'=>$citys);
						  
			/*
			$data = array('typeArr'=>$type, 
			              'province_selected'=>6,
			              'city_selected'=>76,
			              'district_selected'=>693);
			              */
						  
			
			$this->load->view(brief_view, $data);
		}
	}
	
	
	
    
    function do_upload()
    {
		$user_id = $_POST["user_id"];
		
		//$user_id = $this->input->post('user_id', TRUE);
		
		
		if (!isset($user_id) || empty($user_id))
		{
			show_404();
			/*
			session_id($_POST["PHPSESSID"]);
			$this->session->set_userdata('user_id',$_POST["user_id"]);
            $this->session->set_userdata('user_nickname',$_POST["user_nickname"]);
            $this->session->set_userdata('user_in',$_POST["user_in"]);
			*/
		}
		else
		{
			$field_name = 'Filedata';   //上传表单字段名
			$config['upload_path'] = "./upload/";           //文件保存路径
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']    = '2048';
			$config['file_name']   = $user_id;   //存放的文件名
			$config['overwrite'] = true;
			//$config['max_width']  = '1024';
			//$config['max_height']  = '768';
			$this->load->library('upload', $config);
	
			if($this->upload->do_upload($field_name))
			{
				$fileData = $this->upload->data();
				$this->create_thumb($fileData);
			}
		}
    }
	
	
	
	
	
	
	
	  //缩略图
	 public function create_thumb($fileData)
	 {
	 	$this->load->library('image_lib');
		$config['image_library'] = 'gd2';
		$config['source_image'] = './upload/'.$fileData['file_name'];
		$config['overwrite'] = true;
		//$config['new_image']='./upload/'; //有这个参数可以为原来的文件自动备份一个
		$config['create_thumb'] = TRUE;
		$config['maintain_ratio'] = TRUE;
		$config['width'] = 217;
		$config['height'] = 155;
		$this->image_lib->initialize($config); 
		$test = $this->image_lib->resize();
		//var_dump($fileData);
		$img = base_url().'upload/'.$fileData['raw_name']."_thumb".$fileData['file_ext'];      //返回给SWF AND JQUERY显示的IMAGE地址
      	//var_dump($img);
	  
	    echo ('FILEID:'.$img);
	 }
	 
	 
	 
	 
	function detail()
	{
		if(!empty($_POST["existThumb"]))
		{
			$this->load->model(type_model);
			$arr = $this->type_model->selectTypeNameByType($_POST['pro_type']);
			//echo $arr[0]->typeName;
			
			$this->load->model(region_model);
			$province = $this->region_model->select_region_name_by_id($_POST['province_id']);
			$city = $this->region_model->select_region_name_by_id($_POST['city_id']);
			$area = $province[0]->region_name.$city[0]->region_name;
			//echo $area;
			
			
			$arr = array('name'=>$_POST['pro_name'],
			             'state'=>0,
						 'type'=>$_POST['pro_type'],
						 'typeName'=>$arr[0]->typeName,
						 'thumbImg'=>$_POST["existThumb"],
						 'simpleDesc'=>$_POST['pro_desc'],
						 'collectMoney'=>$_POST['pro_money'],
						 'submitDay'=>$_POST['pro_day'],
						 'cityName'=>$area
			            );
			      
			$this->load->model("project_model");
			$id = $this->project_model->insert_brief($arr);
		 	$this->session->set_userdata(array('pro_id'=>$id));
			$this->load->view("detail_view");
		}
		else
		{
			$this->load->view("brief_view");
		}
	}
	
	
	
	
	/*
	
	function detail()
	{
		$config['upload_path']= "./upload/";
		$config['allowed_types']="gif|jpg|png";
		$config['max_size']="200";
		$config['file_name']=$this->session->userdata('user_id');             
        $config['overwrite'] = true;
		$this->load->library("upload",$config);
		if($this->upload->do_upload('img_file'))
		{
			header("Content-type:text/html;charset=utf-8");
			$data=$this->upload->data();
			//var_dump($data);
			
			$this->load->model(type_model);
			$arr = $this->type_model->selectTypeNameByType($_POST['pro_type']);
			//echo $arr[0]->typeName;
			
			$this->load->model(region_model);
			$province = $this->region_model->select_region_name_by_id($_POST['province_id']);
			$city = $this->region_model->select_region_name_by_id($_POST['city_id']);
			$area = $province[0]->region_name.$city[0]->region_name;
			//echo $area;
			
			
			$arr = array('name'=>$_POST['pro_name'],
			             'state'=>0,
						 'type'=>$_POST['pro_type'],
						 'typeName'=>$arr[0]->typeName,
						 'thumbImg'=>$data['file_name'],
						 'simpleDesc'=>$_POST['pro_desc'],
						 'collectMoney'=>$_POST['pro_money'],
						 'submitDay'=>$_POST['pro_day'],
						 'cityName'=>$area
			            );
			      
			$this->load->model("project_model");
			$id = $this->project_model->insert_brief($arr);
		 	$this->session->set_userdata(array('pro_id'=>$id));
			
			
			
		
			
			$this->create_thumb($data);
			
			$this->load->view("detail_view");
			
			
		}
		else
		{
			//$error=array('error'=>$this->upload->display_errors());
			//var_dump($error);
			$this->load->view("brief_view");
		}
	}
	
	*/
	
	
	
	function reward()
	{
		$arr = array('videoUrl'=>$_POST['pro_video_url'],
		             'detailContent'=>($_POST['pro_detail_1'].$_POST['pro_detail_2'].$_POST['pro_detail_3']));
		$this->load->model("project_model");
		
		//echo $this->session->userdata('pro_id')."<br>";
		
		$this->project_model->update_detail($this->session->userdata('pro_id'), $arr);
		
		//var_dump($arr);
		$this->load->view("reward_view");
	}
	
	
	function question()
	{
		$this->load->view("question_view");
	}
	
	function wait()
	{
		$this->load->view("wait_view");
	}
	
	function project()
	{
		//$arr = array('videoUrl'=>$_POST['pro_video_url'],'detailContent'=>$_POST['pro_detail_1'] + $_POST['pro_detail_2'] + $_POST['pro_detail_3']);
		//$this->load->model("project_model");
		
		//echo $this->session->userdata('pro_id')."<br>";
		//var_dump($arr);
		//$this->project_model->update_detail($this->session->userdata('pro_id'), $arr);
		$this->load->view("project_view");
	}
	
	
	
	
	
	
	
}
